package com.bosssoft.boss_exam_db.service.busines.impl;

import com.bosssoft.boss_exam_db.entity.Company;
import com.bosssoft.boss_exam_db.entity.Job;
import com.bosssoft.boss_exam_db.entity.MyGroup;
import com.bosssoft.boss_exam_db.mapper.CompanyMapper;
import com.bosssoft.boss_exam_db.service.base.impl.BaseServiceImpl;
import com.bosssoft.boss_exam_db.service.busines.CompanyService;
import com.bosssoft.boss_exam_db.service.busines.JobService;
import com.bosssoft.boss_exam_db.util.MySqlUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CompanyServiceImpl extends BaseServiceImpl<Company> implements CompanyService {

    //注入CompanyMapper
    @Autowired
    private CompanyMapper companyMapper;

    //注入集团服务
    @Autowired
    private GroupServiceImpl groupService;

    //注入岗位服务
    @Autowired
    private JobService jobService;

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 根据条件查询公司记录
     * @Param [companyName, groupName, page, limit, sort, order] 公司名称 集团名称 分页信息
     * @Return com.github.pagehelper.PageInfo<com.bosssoft.boss_exam_db.entity.Company>
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public PageInfo<Company> queryByCondition(String companyName, String groupName, Integer page, Integer limit, String sort, String order) {
        Example example = new Example(Company.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(companyName)) {
            criteria.andLike("companyName", MySqlUtil.cloumLike(companyName));
        }
        if (!StringUtils.isEmpty(groupName)) {
            Example groupExample = new Example(MyGroup.class);
            Example.Criteria groupExampleCriteria = groupExample.createCriteria();
            groupExampleCriteria.andLike("groupname", MySqlUtil.cloumLike(groupName));
            List<MyGroup> myGroups = groupService.selectByExample(groupExample);
            List<String> groupIds = myGroups.stream().map(group -> group.getId()).collect(Collectors.toList());
            if(groupIds.size()!=0&&groupIds!=null){
                criteria.andIn("groupId", groupIds);
            }
        }
        criteria.andEqualTo("deleted",false);
        PageInfo<Company> companyPageInfo = PageHelper.startPage(page, limit).setOrderBy(sort + " " + order).doSelectPageInfo(() ->
                this.companyMapper.selectByExample(example)
        );
        return companyPageInfo;
    }

    /***
     * @Author lys(cherish)
     * @Phone 17720799636
     * @Date  2019/4/8
     * @Description 级联删除公司及公司下的岗位
     * @Param [company]
     * @Return int
     * @Exception
     * @since JDK 1.8
     **/
    @Override
    public int deleteCascade(Company company) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        //删除公司
        int companyFlag = this.deleteForLogic(company);
        //删除公司下的岗位
        Example jobExample = new Example(Job.class);
        Example.Criteria jobExampleCriteria = jobExample.createCriteria();
        jobExampleCriteria.andEqualTo("companyId",company.getId());
        Job job = new Job();
        job.setDeleted(true);
        int jobFlag = jobService.deleteForLogicByExample(job, jobExample);
        // todo 考核科目以后要删
        if(companyFlag==1&&jobFlag>=0){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public List<Company> queryAllCompanyByGroupId(String groupId) {
        Example example = new Example(Company.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isEmpty(groupId)){
            return null;
        }
        criteria.andEqualTo("deleted",false);
        criteria.andEqualTo("groupId",groupId);
        List<Company> companyList = this.companyMapper.selectByExample(example);
        return companyList;
    }

}
